import { http } from "@/utils/http";
import { jobUrlApi } from "@/api/utils";
import type { StringPage } from "@/views/job/instance-management/utils/types";

// type Result = {
//   success: boolean;
//   data?: Array<any>;
// };

export type ResultTable = {
  /** 列表数据 */
  records: Array<any>;
  /** 总条目数 */
  total?: number;
  /** 每页显示条目个数 */
  size?: number;
  /** 当前页数 */
  current?: number;
};

export type AppInfo = {
  /**
   * ID
   */
  id: string;

  /**
   * 业务应用名称
   */
  name: string;

  /**
   * 业务应用编号
   */
  code: string;

  /**
   * 排序
   */
  sort?: number;

  /**
   * 业务应用描述
   */
  description?: string;

  /**
   * 业务应用域名
   */
  host: string;

  /**
   * 请求上下文路径
   */
  contextPath?: string;

  /**
   * 业务应用密钥
   */
  secret?: string;

  /**
   * 开始时间
   */
  startTime?: Date;

  /**
   * 结束时间
   */
  endTime?: Date;

  /**
   * 状态
   */
  status?: string;

  /**
   * 当前管理Job Server IP端口
   */
  currentServer?: string;
};

export type InstanceDetails = {
  id: string;
  /**
   * 任务预计执行时间
   */
  expectedTriggerTime: number;
  /**
   * 任务整体开始时间
   */
  actualTriggerTime: number;
  /**
   * 任务整体结束时间（可能不存在）
   */
  finishedTime?: string;
  /**
   * 任务状态
   */
  status: string;
  /**
   * 任务执行结果（可能不存在）
   */
  result?: string;
  /**
   * TaskTracker地址
   */
  taskTrackerAddress: string;
  /**
   * 任务参数
   */
  jobParams: string;
  /**
   * 启动参数
   */
  instanceParams: string;
  /**
   * 重试次数
   */
  runningTimes: number;
};

export type LogConfig = {
  type: string;
  level: string;
};

export type InstanceInfo = {
  id: string;
  appId: string;
  jobName: string;
  jobDescription: string;
  jobParams: string;
  timeExpressionType: string;
  timeExpression: string;
  processorType: string;
  processorInfo: string;
  status: string;
  instanceTimeLimit: number;
  retryTimes: number;
  lifecycleRange: Array<string>;
  logConfig: LogConfig;
};

export type InstanceDetailsReq = {
  appId: string;
  instanceId: string;
};

export const getAppInfoPage = (params?: object) => {
  return http.request<ResultTable>(
    "get",
    jobUrlApi("/app-info/v1/app-info/page"),
    {
      params
    }
  );
};

export const getAppList = () => {
  return http.request<Array<AppInfo>>(
    "get",
    jobUrlApi("/app-info/v1/app-info/list")
  );
};

export const addAppInfo = (data?: object) => {
  return http.request<boolean>("post", jobUrlApi("/app-info/v1/app-info"), {
    data
  });
};

export const updateAppInfo = (data?: object) => {
  return http.request<boolean>("put", jobUrlApi("/app-info/v1/app-info"), {
    data
  });
};

export const deleteAppInfo = (id?: string) => {
  return http.request<boolean>(
    "delete",
    jobUrlApi("/app-info/v1/app-info/" + id)
  );
};

export const modifyAppInfoStatus = (data?: object) => {
  return http.request<boolean>(
    "put",
    jobUrlApi("/app-info/v1/app-info/status"),
    { data }
  );
};

export const resetSecret = (id?: string) => {
  return http.request<boolean>(
    "put",
    jobUrlApi("/app-info/v1/app-info/reset-secret/" + id)
  );
};

export const getJobInfoPage = (params?: object) => {
  return http.request<ResultTable>(
    "get",
    jobUrlApi("/job-info/v1/job-info/page"),
    {
      params
    }
  );
};

export const saveJob = (data?: object) => {
  return http.request<boolean>(
    "post",
    jobUrlApi("/job-info/v1/job-info/save"),
    {
      data
    }
  );
};

export const modifyJobStatus = (data?: object) => {
  return http.request<boolean>(
    "put",
    jobUrlApi("/job-info/v1/job-info/status"),
    { data }
  );
};

export const validateTimeExpression = (params?: object) => {
  return http.request<Array<string>>(
    "get",
    jobUrlApi("/job-info/v1/job-info/time-expression/validate"),
    {
      params
    }
  );
};

export const runJob = (data?: object) => {
  return http.request<boolean>(
    "post",
    jobUrlApi("/job-info/v1/job-info/run-job"),
    {
      data
    }
  );
};

export const getJobList = (data?: object) => {
  return http.request<Array<any>>(
    "post",
    jobUrlApi("/job-info/v1/job-info/list"),
    {
      data
    }
  );
};

export const getInstancePage = (params?: object) => {
  return http.request<ResultTable>(
    "get",
    jobUrlApi("/instance/v1/instance/page"),
    {
      params
    }
  );
};

export const getInstanceDetails = (data?: InstanceDetailsReq) => {
  return http.request<InstanceDetails>(
    "get",
    jobUrlApi(
      "/instance/v1/instance/detail/" + data.appId + "/" + data.instanceId
    ),
    null,
    {
      headers: {
        "app-id": data.appId
      }
    }
  );
};

export const getInstanceLog = (params?: object, appId?: string) => {
  return http.request<StringPage>(
    "get",
    jobUrlApi("/instance/v1/instance/log"),
    {
      params
    },
    {
      headers: {
        "app-id": appId
      }
    }
  );
};

export const onRetryInstance = (data?: InstanceInfo) => {
  return http.request<StringPage>(
    "post",
    jobUrlApi("/instance/v1/instance/retry/" + data.id),
    null,
    {
      headers: {
        "app-id": data.appId
      }
    }
  );
};

export const onStopInstance = (data?: InstanceInfo) => {
  return http.request<StringPage>(
    "put",
    jobUrlApi("/instance/v1/instance/stop/" + data.id),
    null,
    {
      headers: {
        "app-id": data.appId
      }
    }
  );
};
